<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商家Id 与shopping_user中id关联" prop="storeId">
      <el-input v-model="dataForm.storeId" placeholder="商家Id 与shopping_user中id关联"></el-input>
    </el-form-item>
    <el-form-item label="优惠劵名称" prop="title">
      <el-input v-model="dataForm.title" placeholder="优惠劵名称"></el-input>
    </el-form-item>
    <el-form-item label="满足此金额才能减" prop="fullMoney">
      <el-input v-model="dataForm.fullMoney" placeholder="满足此金额才能减"></el-input>
    </el-form-item>
    <el-form-item label="能够减多少金额" prop="money">
      <el-input v-model="dataForm.money" placeholder="能够减多少金额"></el-input>
    </el-form-item>
    <el-form-item label="优惠券开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="优惠券开始时间"></el-input>
    </el-form-item>
    <el-form-item label="优惠券结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="优惠券结束时间"></el-input>
    </el-form-item>
    <el-form-item label="状态，0表示暂停发行，1表示发行中" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态，0表示暂停发行，1表示发行中"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="优惠劵描述" prop="describe">
      <el-input v-model="dataForm.describe" placeholder="优惠劵描述"></el-input>
    </el-form-item>
    <el-form-item label="优惠劵发放方式：0表示购劵中心，1表示消费赠送" prop="issue">
      <el-input v-model="dataForm.issue" placeholder="优惠劵发放方式：0表示购劵中心，1表示消费赠送"></el-input>
    </el-form-item>
    <el-form-item label="当优惠劵发放方式为1，消费满足此金额才发放" prop="fullGet">
      <el-input v-model="dataForm.fullGet" placeholder="当优惠劵发放方式为1，消费满足此金额才发放"></el-input>
    </el-form-item>
    <el-form-item label="发行量" prop="issueAmount">
      <el-input v-model="dataForm.issueAmount" placeholder="发行量"></el-input>
    </el-form-item>
    <el-form-item label="剩余数量" prop="residualAmount">
      <el-input v-model="dataForm.residualAmount" placeholder="剩余数量"></el-input>
    </el-form-item>
    <el-form-item label="商家优惠劵被已领数量" prop="getAmount">
      <el-input v-model="dataForm.getAmount" placeholder="商家优惠劵被已领数量"></el-input>
    </el-form-item>
    <el-form-item label="商家优惠劵被已使用数量" prop="useAmount">
      <el-input v-model="dataForm.useAmount" placeholder="商家优惠劵被已使用数量"></el-input>
    </el-form-item>
    <el-form-item label="审核：0表示审核不通过，1表示审核通过，3表示审核中" prop="check">
      <el-input v-model="dataForm.check" placeholder="审核：0表示审核不通过，1表示审核通过，3表示审核中"></el-input>
    </el-form-item>
    <el-form-item label="审核不通过原因" prop="checkNoPass">
      <el-input v-model="dataForm.checkNoPass" placeholder="审核不通过原因"></el-input>
    </el-form-item>
    <el-form-item label="发行此优惠劵消耗金额=发行量*0.1" prop="consumeMoney">
      <el-input v-model="dataForm.consumeMoney" placeholder="发行此优惠劵消耗金额=发行量*0.1"></el-input>
    </el-form-item>
    <el-form-item label="删除标记" prop="deletestatus">
      <el-input v-model="dataForm.deletestatus" placeholder="删除标记"></el-input>
    </el-form-item>
    <el-form-item label="优惠劵图片" prop="pic">
      <el-input v-model="dataForm.pic" placeholder="优惠劵图片"></el-input>
    </el-form-item>
    <el-form-item label="购买优惠劵价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="购买优惠劵价格"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          storeId: '',
          title: '',
          fullMoney: '',
          money: '',
          startTime: '',
          endTime: '',
          status: '',
          createTime: '',
          describe: '',
          issue: '',
          fullGet: '',
          issueAmount: '',
          residualAmount: '',
          getAmount: '',
          useAmount: '',
          check: '',
          checkNoPass: '',
          consumeMoney: '',
          deletestatus: '',
          pic: '',
          price: ''
        },
        dataRule: {
          storeId: [
            { required: true, message: '商家Id 与shopping_user中id关联不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '优惠劵名称不能为空', trigger: 'blur' }
          ],
          fullMoney: [
            { required: true, message: '满足此金额才能减不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '能够减多少金额不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '优惠券开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '优惠券结束时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态，0表示暂停发行，1表示发行中不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          describe: [
            { required: true, message: '优惠劵描述不能为空', trigger: 'blur' }
          ],
          issue: [
            { required: true, message: '优惠劵发放方式：0表示购劵中心，1表示消费赠送不能为空', trigger: 'blur' }
          ],
          fullGet: [
            { required: true, message: '当优惠劵发放方式为1，消费满足此金额才发放不能为空', trigger: 'blur' }
          ],
          issueAmount: [
            { required: true, message: '发行量不能为空', trigger: 'blur' }
          ],
          residualAmount: [
            { required: true, message: '剩余数量不能为空', trigger: 'blur' }
          ],
          getAmount: [
            { required: true, message: '商家优惠劵被已领数量不能为空', trigger: 'blur' }
          ],
          useAmount: [
            { required: true, message: '商家优惠劵被已使用数量不能为空', trigger: 'blur' }
          ],
          check: [
            { required: true, message: '审核：0表示审核不通过，1表示审核通过，3表示审核中不能为空', trigger: 'blur' }
          ],
          checkNoPass: [
            { required: true, message: '审核不通过原因不能为空', trigger: 'blur' }
          ],
          consumeMoney: [
            { required: true, message: '发行此优惠劵消耗金额=发行量*0.1不能为空', trigger: 'blur' }
          ],
          deletestatus: [
            { required: true, message: '删除标记不能为空', trigger: 'blur' }
          ],
          pic: [
            { required: true, message: '优惠劵图片不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '购买优惠劵价格不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shoppingcoupon/shoppingstorecoupon/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.storeId = data.shoppingstorecoupon.storeId
                this.dataForm.title = data.shoppingstorecoupon.title
                this.dataForm.fullMoney = data.shoppingstorecoupon.fullMoney
                this.dataForm.money = data.shoppingstorecoupon.money
                this.dataForm.startTime = data.shoppingstorecoupon.startTime
                this.dataForm.endTime = data.shoppingstorecoupon.endTime
                this.dataForm.status = data.shoppingstorecoupon.status
                this.dataForm.createTime = data.shoppingstorecoupon.createTime
                this.dataForm.describe = data.shoppingstorecoupon.describe
                this.dataForm.issue = data.shoppingstorecoupon.issue
                this.dataForm.fullGet = data.shoppingstorecoupon.fullGet
                this.dataForm.issueAmount = data.shoppingstorecoupon.issueAmount
                this.dataForm.residualAmount = data.shoppingstorecoupon.residualAmount
                this.dataForm.getAmount = data.shoppingstorecoupon.getAmount
                this.dataForm.useAmount = data.shoppingstorecoupon.useAmount
                this.dataForm.check = data.shoppingstorecoupon.check
                this.dataForm.checkNoPass = data.shoppingstorecoupon.checkNoPass
                this.dataForm.consumeMoney = data.shoppingstorecoupon.consumeMoney
                this.dataForm.deletestatus = data.shoppingstorecoupon.deletestatus
                this.dataForm.pic = data.shoppingstorecoupon.pic
                this.dataForm.price = data.shoppingstorecoupon.price
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shoppingcoupon/shoppingstorecoupon/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'storeId': this.dataForm.storeId,
                'title': this.dataForm.title,
                'fullMoney': this.dataForm.fullMoney,
                'money': this.dataForm.money,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'describe': this.dataForm.describe,
                'issue': this.dataForm.issue,
                'fullGet': this.dataForm.fullGet,
                'issueAmount': this.dataForm.issueAmount,
                'residualAmount': this.dataForm.residualAmount,
                'getAmount': this.dataForm.getAmount,
                'useAmount': this.dataForm.useAmount,
                'check': this.dataForm.check,
                'checkNoPass': this.dataForm.checkNoPass,
                'consumeMoney': this.dataForm.consumeMoney,
                'deletestatus': this.dataForm.deletestatus,
                'pic': this.dataForm.pic,
                'price': this.dataForm.price
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
